﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Memory;
using Elsa.Workflows.Models;
using Newtonsoft.Json;
using T.FAS.Runtime.MF.Activities;
using T.FAS.Runtime.MF.Activities.Helper;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.SPI;

namespace T.FAS.Runtime.MF.Activities.Nodes
{
    // 结束节点
    public class EndNode : NodeBase, INodeMapper
    {
        [JsonProperty("data")]
        public new EndNodeData Data { get; set; }
        public List<Variable> Variables { get; set; } = new List<Variable>();
        public List<OutputDefinition> Outputs { get; set; }=new List<OutputDefinition>();
        public List<InputDefinition> Inputs { get; set; } = new List<InputDefinition>();
        public IActivity MapToActivity()
        {
            var activity = new FasEndActivity() { Id=Id,  Name = Data.Label };
           
            activity.VariablesToSetWorkflowOutput = new List<EndOutputVariable>();

            foreach (var variable in (Data.OutputVariables ?? new List<UserVariable>()))
            {
                //输出变量的名称
                var outputVarName = variable.VariableName;
                //来源变量名称
                var sourceVariableName = variable.VariableValue;
                //来源变量所属的节点ID
                var sourceVariableNodeID = string.IsNullOrEmpty(variable.VariableNodeID) ? variable.Reference?.Split('.')[0] : variable.VariableNodeID;
                //如果节点参数的来源于迭代节点的output，则取out对应的节点的参数，不直接使用output
                if (sourceVariableName == "output" && sourceVariableNodeID.StartsWith("iteration"))
                {
                    // var outputVarInfo = (this.BelongTo.NodeInputOutputVariables ?? new List<UserVariable>()).Find(t => t.VariableName == variable.VariableName && t.ActivityID == variable.NodeID);
                    var outputVarInfo = ActivityHelper.GetSourceVariable(this, $"{sourceVariableName}", sourceVariableNodeID);
                    if (outputVarInfo != null)
                    {
                        //variable.VariableName = outputVarInfo.OriginalVariableValue;
                        //variable.NodeID = outputVarInfo.VariableNodeID;
                        //variable.Type = outputVarInfo.Type;
                        sourceVariableName = outputVarInfo.OriginalVariableValue;
                        sourceVariableNodeID = outputVarInfo.VariableNodeID;
                    }
                    var sourceVariable2 = ActivityHelper.GetSourceVariable(this, sourceVariableName, sourceVariableNodeID);
                    if (sourceVariable2 != null)
                    {
                        activity.VariablesToSetWorkflowOutput.Add(
                       new EndOutputVariable
                       {
                           VarName = outputVarName,
                           SourceNode = sourceVariableNodeID,
                           // SourceVariable = variable.SourceVariable, 
                           //这里要取变量的复合名称，Start节点的变量名称，是原始变量名称，其它节点的变量名称，需要带上一个ActivityID,防止重名
                           SourceVariable = $"{sourceVariable2.CompositeVarName}{MFConst.IterationResultVariableSuffix}",
                           Description = variable.Description,
                           SourceVarType = sourceVariable2?.Type,
                           ActivityID = variable.ActivityID,
                       });
                    }
                }
                else
                {
                    var sourceVariable = ActivityHelper.GetSourceVariable(this, sourceVariableName, sourceVariableNodeID);
                    activity.VariablesToSetWorkflowOutput.Add(
                        new EndOutputVariable
                        {
                            VarName = outputVarName,
                            SourceNode = sourceVariableNodeID,
                            // SourceVariable = variable.SourceVariable, 
                            //这里要取变量的复合名称，Start节点的变量名称，是原始变量名称，其它节点的变量名称，需要带上一个ActivityID,防止重名
                            SourceVariable = sourceVariable?.CompositeVarName,
                            Description = variable.Description,
                            SourceVarType = sourceVariable?.Type,
                            ActivityID = variable.ActivityID,
                        });
                }
                    //var sourceVariable = this.BelongTo.NodeInputOutputVariables.Find(t => t.VariableName == variable.SourceVariable && t.ActivityID == variable.SourceNode );
               

                //将结束结点设置的输出变量定义为工作流的Output
                Outputs.Add(new OutputDefinition { Type=typeof(object), Name= outputVarName, Description=variable.Description });
            }
            activity.Metadata = DesignerMeta;
            return activity;
        }
    }


    public class EndNodeData
    {
        [JsonProperty("label")]
        public string Label { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("outputFields")]
        public List<UserVariable> OutputVariables { get; set; }

        // 其他结束节点特定属性
    }

}
